<?php

date_default_timezone_set("Europe/Vienna");

require 'php/log.php';


function customError($error_level,$error_message,$error_file,$error_line,$error_context)
{	// All errors should be written to log file.
	$line="er:".$error_level.":".$error_message.": in source ".basename($error_file).": on line ".$error_line;
	mail("j.stevens@asim.at", "php server error", $line);
	die($line);
}

set_error_handler("customError");

function do_op($op)
{

	switch($op)
	{
	case "backup":
		break;
	case "err":
	case "log":	// Normal log file upload
		log_upload($_FILES['file']['tmp_name'],$_REQUEST['dest'],$_REQUEST['product'],$_REQUEST['machineid'],$_REQUEST['version']);
		if($_REQUEST["op"]=="err")
			log_send($_REQUEST['machineid'],$_REQUEST['product']);
		break;
	case "login":
		break;
	case "public": // Get the public key
		echo(file_get_contents("App_Data/public.pem"));
		break;
	case "setreleasetype":
		include 'php/setreleasetype.php';
		break;
	case "upload":
		// File upload example script
		$version = "v".implode("_",explode(".",$_REQUEST['version']));
		$file = str_replace("\\","/",$_REQUEST['dest']);
		$target = trim(str_replace("\\","/","products/".$_REQUEST['product']."/".$version."/".dirname($file)),".");
		if(!is_dir($target))
			mkdir($target,0777,true);
		if(substr($target,-1)!='/')	$target .= '/';
		$target .= basename($file);
		$ok=1;
		// Move the file
		if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
			echo "ok:0:The file ". basename($file). " has been uploaded";
		}
		break;
	case "ver": // Version of this script - 1
		echo("ok:0:1");
		break;
	case "email":
		include 'php/mail.php';
		echo send_maillist($_REQUEST['dest'],$_REQUEST['subject'],$_REQUEST['body'],false);
		break;
	case "test":
		include 'php/test.php';
		break;
	default:
		trigger_error("No input command set");
		break;
	}
}

do_op(array_key_exists("op",$_REQUEST)?$_REQUEST["op"]:"test");

?> 
